<?php
    error_reporting(0);
    require '../../../inc/config/config.php';
    require '../../../inc/library/functions.php';
	$error = "";
	$msg = " ";
    $inputfilename=$_POST["inputname"];//文件域名称
	$filetmpname=$_FILES[$inputfilename]['tmp_name'];
	$filename=$_FILES[$inputfilename]['name'];
    $wx_num=$_POST["wx_num"];//默认微信号码
    $type=$_POST["type"];//类型,1表示头像,2表示二维码

    $uploadpath=$type==1?UPLOADHEADPATH:UPLOADCODEPATH;
    $hashfolder=ezmlm_hash($wx_num);
    $filepath="/".$uploadpath."/".$hashfolder."/";
	$uploads=dirname(__FILE__).$filepath;

    @mkdirs($uploads);
    $ext=strtolower(substr($filename,strrpos($filename,".")+1));

    if(!empty($_FILES['ToUpload']['error'])){
		switch($_FILES['ToUpload']['error'])
		{

			case '1':
				$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
				break;
			case '2':
				$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
				break;
			case '3':
				$error = 'The uploaded file was only partially uploaded';
				break;
			case '4':
				$error = '请选择要上传的文件';
				break;

			case '6':
				$error = 'Missing a temporary folder';
				break;
			case '7':
				$error = 'Failed to write file to disk';
				break;
			case '8':
				$error = 'File upload stopped by extension';
				break;
			case '999':
			default:
				$error = 'No error code avaiable';
		}
	}else if($ext!= 'gif' and $ext!= 'jpg' and $ext!= 'png' and $ext!="bmp"){
		$error = '只允许上传jpg,png,gif,bmp等格式文件';
	}else {
			//$msg .= " File Name: " . $_FILES['fileToUpload']['name'] . ", ";
			//$msg .= " File Size: " . @filesize($_FILES['fileToUpload']['tmp_name']);
			//for security reason, we force to remove all uploaded file
			//@unlink($_FILES['fileToUpload']);
			$names=$wx_num.".".$ext;
			$newfilename=$uploads.$names;
			if(move_uploaded_file($filetmpname,$newfilename)){
				$msg.="上传成功";
				$result=$hashfolder."/".$names;
                //ImageResize($newfilename,220,168,$uploads.$tumbnailnames);
			}else{
				$msg.="上传失败";
			}
				 		
	}
    $result=array("error"=>$error,"msg"=>$msg,"results"=>$result);
    echo json_encode($result);
?>